package top.lidee.utils;

import cn.hutool.core.collection.ConcurrentHashSet;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.checkerframework.checker.units.qual.A;

import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description 15位唯一数字
 * @author zhangyiheng
 * @date 2024/9/27 00:43
 */
public class CustomIdGenerator implements IdentifierGenerator {
 
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
 
    // 控制为15位
    private static int ID_LENGTH = 15;
 
    @Override
    public Number nextId(Object entity) {
        //  生成最大4位随技术
        int i2 = ThreadLocalRandom.current().nextInt(9999);
        String timeStr = String.valueOf(System.currentTimeMillis());
        // 取出时间串前面相同的部分
        timeStr = timeStr.substring(5);
        // 递增生成最大9999的递增ID
        if (atomicInteger.get() == 9999) {
            atomicInteger.set(0);
        }
        int i1 = atomicInteger.getAndIncrement();
        String id = timeStr.concat(String.valueOf(i2)).concat(i1+"");
        // 严格控制ID长度，如果过长 从最前面截取
        if (id.length() > ID_LENGTH) {
            // 计算多了多少位
            int surplusLenth = id.length() - ID_LENGTH;
            id = id.substring(surplusLenth);
        }
        return Long.valueOf(id);
    }

    public static String getFifteenAUniqueDigit(){
        CustomIdGenerator customIdGenerator=new CustomIdGenerator();
        customIdGenerator.nextId(null).longValue();
        Number number = customIdGenerator.nextId(new Object());
        return number.toString();
    }


}